package com.zzyl.nursing.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.nursing.service.WechatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 *
 * @Author mr.wu
 * @Data #{DATE} #{TIME}
 */
@Service
public class WechatServiceImpl implements WechatService {

    @Value("${wechat.appid}")
    private String appId;
    @Value("${wechat.appSecret}")
    private String appSecret;


    private static final String GET_OPID_URL = "https://api.weixin.qq.com/sns/jscode2session";

    private static final String GET_ACCESS_TOKEN = "https://api.weixin.qq.com/cgi-bin/token";

    private static final String POST_PHONE_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";

    public Map<String,Object> getParmer(){
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("appid",appId);
        paramMap.put("secret",appSecret);
        return paramMap;
    }

    /**
     * 通过微信Api发送请求获取openId
     * @param code
     * @return
     */
    @Override
    public String getOpenId(String code) {
        //1.封装测试参数
        Map<String, Object> parmerMap = this.getParmer();
        parmerMap.put("js_code", code);
        parmerMap.put("grant_type", "authorization_code");
        //2.发送Api请求
        String result = HttpUtil.get(GET_OPID_URL, parmerMap);
        if(StringUtils.isBlank(result)){
            throw new BaseException("调用微信获取openId的接口失败");
        }

        //3.解析Json参数
        JSONObject jsonObject = JSONObject.parseObject(result);
        Integer errcode = jsonObject.getInteger("errcode");
        if(errcode!=null && errcode!=0){
            throw new BaseException("调用微信获取openId的接口失败");
        }
        String openId = jsonObject.getString("openid");
        return openId;
    }

    /**
     *  通过微信Api发送请求获取用户手机号
     * @param code
     * @return
     */
    @Override
    public String getPhone(String code) {
        //1.准备测试参数
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("code",code);
        String accessToken = this.getToken();
        //2.发送请求
        String result = HttpUtil.post(POST_PHONE_URL + accessToken, JSON.toJSONString(paramMap));
        if(StringUtils.isBlank(result)){
            throw new BaseException("获取手机号失败");
        }
        //3.解析json参数
        JSONObject jsonObject = JSONObject.parseObject(result);
        Integer errcode = jsonObject.getInteger("errcode");
        if(errcode!=null && errcode!=0){
            throw new BaseException("通过微信Api发送请求获取用户手机号失败");
        }
        String phoneInfoJSON = jsonObject.getString("phone_info");
        if(StringUtils.isNotBlank(phoneInfoJSON)) {
            JSONObject phoneObject = JSONObject.parseObject(phoneInfoJSON);
            return phoneObject.getString("phoneNumber");
        }
        return null;
    }

    /**
     *  通过微信Api发送请求获取Access_token
     * @param
     * @return
     */
    private String getToken(){
        //1.封装测试参数
        Map<String, Object> parmerMap = this.getParmer();
        parmerMap.put("grant_type", "client_credential");
        //2.调用Api接口
        String result = HttpUtil.get(GET_ACCESS_TOKEN, parmerMap);
        if(StringUtils.isBlank(result)){
            throw new BaseException("通过微信Api发送请求获取Access_token失败");
        }
        //3.解析Json参数
        JSONObject jsonObject = JSONObject.parseObject(result);
        String token = jsonObject.getString("access_token");
        return token;
    }
}
